<?php
namespace app\home\controller;

use app\common\MorderFinanceModel;
use app\common\MorderModel;
use think\Request;
use think\Validate;

class FinanceController extends BaseController
{
    private $financeModel;
    private $orderModel;
    private $userInfo;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->financeModel=new MorderFinanceModel();
        $this->orderModel=new MorderModel();
        $this->userInfo=$this->getDduser();
    }
    //获取财务列表
    public function index()
    {
        $data=$this->request->param();
        $map=[];
        foreach ($data as $index=>$value) {
            switch ($index){
                case 'order_id':
                case 'order_sn':
                case 'customer_id':
                case 'finance_type':
                case 'create_userid':
                case 'check_userid':
                case 'check_result':
                case 'income_or_expenses':
                    $map[$index]=$value;
                    break;
                case 'finance_name':
                case 'order_name':
                    $map[$index]=['like','%'.$index.'%'];
                    break;
            }
        }
        $createStartTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $createEndTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($createStartTime), strtotime($createEndTime)]];
        $checkStartTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $checkEndTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($checkStartTime), strtotime($checkEndTime)]];
        $list=$this->financeModel->where($map)->paginate(12,false,['query'=>$data]);
        return $this->callBack(0,'',$list);
    }

    /**
     * 获取某条件下款项总和
     */
    public function getTotalMoney()
    {
        $data=$this->request->param();
        $map=[];
        foreach ($data as $index=>$item){
            if (in_array($index,['order_id','order_sn','customer_id','finance_type','create_userid','check_userid','check_result','income_or_expenses'])){
                $map[$index]=$item;
            }
            if (in_array($index,['finance_type','order_name'])){
                $map[$index]=['like','%'.$item.'%'];
            }
        }
        $createStartTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $createEndTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($createStartTime), strtotime($createEndTime)]];
        $checkStartTime = $this->request->param('start_time', '1970-01-01 08:00:01');
        $checkEndTime = $this->request->param('end_time', '5138-11-16 17:46:39');
        $map['create_time'] = ['between', [strtotime($checkStartTime), strtotime($checkEndTime)]];
        if (empty($map['check_result'])){
            $map['check_result']=1;
        }
        $lists=$this->financeModel->where($map)->field('money,plan_money,income_or_expenses')->select();
        $money=0;
        $planMoney=0;
        foreach ($lists as $list) {
            $money+=(intval($list['income_or_expenses'])/abs(intval($list['income_or_expenses'])))*$list['money'];
            $planMoney+=(intval($list['income_or_expenses'])/abs(intval($list['income_or_expenses'])))*$list['plan_money'];
        }
        return $this->callBack(0,'',['money'=>$money,'plan_money'=>$planMoney]);
    }

    /**
     * 录入款项
     * @return array|\think\response\Json
     */
    public function add()
    {
        $data=$this->request->param();
        $data['create_userid']=$this->userInfo['userid'];
        $validate=new Validate([
            ['order_id','require','请选择订单'],
            ['finance_name','require','请填写款项名称'],
            ['finance_type','require','请填写收款方式'],
            ['create_userid','require','请先登录'],
            ['plan_money','require','请填写应付款金额'],
            ['money','require','请填写是收款金额'],
            ['income_or_expenses','require','请选择收入还是支出']
        ]);
        $result=$validate->check($data);
        if (true!==$result){
            return $this->callBack(1,$validate->getError());
        }

        $orderInfo=$this->orderModel->where(['order_id',$data['order_id']])->find();
        if (empty($orderInfo)){
            return $this->callBack(1,'请选择正确的订单');
        }
        $data['order_sn']=$orderInfo['order_sn'];
        $data['order_name']=$orderInfo['order_name'];
        $data['customer_id']=$orderInfo['customer_id'];
        $data['check_result']=0;
        if ($this->financeModel->data($data)->save()){
            return $this->callBack(0,'款项录入成功',$this->financeModel->getLastInsID());
        }else{
            return $this->callBack(2,$this->financeModel->getError());
        }
    }
    /**
     * 修改款项
     * @return array|\think\response\Json
     */
    public function edit()
    {
        $data=$this->request->param();
        $data['create_userid']=$this->userInfo['userid'];
        $validate=new Validate([
            ['finance_id','require','请选择要修改的款项'],
            ['order_id','require','请选择订单'],
            ['finance_name','require','请填写款项名称'],
            ['finance_type','require','请填写收款方式'],
            ['create_userid','require','请先登录'],
            ['plan_money','require','请填写应付款金额'],
            ['money','require','请填写是收款金额'],
            ['income_or_expenses','require','请选择收入还是支出']
        ]);
        $result=$validate->check($data);
        if (true!==$result){
            return $this->callBack(1,$validate->getError());
        }
        $financeInfo=$this->financeModel->where('finance_id',$data['finance_id'])->find();
        if ($financeInfo['check_result']!=0){
            return $this->callBack(1,'款项已经经过审核,不可修改');
        }
        $orderInfo=$this->orderModel->where(['order_id',$data['order_id']])->find();
        if (empty($orderInfo)){
            return $this->callBack(1,'请选择正确的订单');
        }
        $data['order_sn']=$orderInfo['order_sn'];
        $data['order_name']=$orderInfo['order_name'];
        $data['customer_id']=$orderInfo['customer_id'];
        $data['check_result']=0;
        if ($this->financeModel->allowField(true)->data($data)->save()){
            return $this->callBack(0,'款项修改成功',$this->financeModel->getLastInsID());
        }else{
            return $this->callBack(2,$this->financeModel->getError());
        }
    }

    /**
     * 删除款项
     * @return array|\think\response\Json
     */
    public function del(){
        $financeId=$this->request->param('finance_id',0);
        $financeInfo=$this->financeModel->where('finance_id',$financeId)->find();
        if ($financeInfo['check_result']!=0){
            return $this->callBack(1,'款项已经经过审核,不可删除');
        }
        if ($this->financeModel->where('finance_id',$financeId)->delete()){
            return $this->callBack(0,'款项删除成功');
        }else{
            return $this->callBack(2,$this->financeModel->getError());
        }
    }

    /**
     * 审核款项
     */
    public function check()
    {
        $data=$this->request->param();
        $validate=new Validate([
            ['finance_id','require','请选择要审核的款项'],
            ['check_result','require','请选择审核后的状态'],
        ]);
        $result=$validate->check($data);
        if (true!==$result){
            return $this->callBack(1,$validate->getError());
        }
        if ($this->financeModel->save(['finance_id'=>$data['finance_id'],'check_userid'=>$this->userInfo['userid'],'check_time'=>time(),'check_result'=>$data['check_result']])){
            return $this->callBack(0,'审核成功');
        }else{
            return $this->callBack(1,$this->financeModel->getError());
        }
    }


}
